<template>
  <div class="main">
    <el-button class="add" type="primary" round
               @click="dialogVisible = true">新增菜品
    </el-button>
    <div class="form">
      <el-table :data="tableData">
        <el-table-column label="id" prop="id"></el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="单价" prop="price"></el-table-column>
        <el-table-column label="分类" prop="category_id"></el-table-column>
        <el-table-column>
          <template slot-scope="scope">
            <el-button size="mini" type="danger"
                       @click="removeDish(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
        title="新增菜品"
        :visible.sync="dialogVisible"
        width="30%">
      <el-form>
        <el-form-item label="名称">
          <el-input v-model="name"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input type="textarea" :rows="2" v-model="description"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="price"></el-input>
        </el-form-item>
        <el-form-item label="分类">
          <el-select v-model="category" @change="getCategoryId">
            <el-option v-for="category in categoryList" :key="category.id"
                       :label="category.name" :value="category.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="newDish">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {allCategory} from "@/http/api/category";
import {addDish, allDish, deleteDish} from "@/http/api/restaurant";
import cookie from "js-cookie";

export default {
  name: "Dish",
  data() {
    return {
      tableData: [],
      categoryList: [],

      price: 0,

      categoryId: '',
      categoryName: '',
      name: '',
      description: '',

      category: '1',

      dialogVisible: false, // 对话框
    }
  },
  methods: {

    // 获取所有的菜式
    async getCategory() {
      let result = await allCategory()
      if (result.code === 0) {
        this.categoryList = result.data
      }
    },

    // 获取所有菜品
    async getDish() {
      let result = await allDish({
        restaurantId: cookie.get('restaurantId')
      })
      if (result.code === 0) {
        let data = result.data
        if (data) {
          this.tableData = data
        }
      }
    },

    // 新增菜品
    async newDish() {
      let result = await addDish({
        restaurantId: cookie.get('restaurantId'),
        categoryId: this.categoryId,
        name: this.name,
        description: this.description,
        price: this.price,
      })
      if (result.code === 0) {
        this.$message.success("添加成功")
        await this.getDish()
      } else {
        this.$message.warning(result.message)
      }
      this.dialogVisible = false
    },

    // 删除指定菜品
    async removeDish(dishId) {
      let result = await deleteDish({
        dishId: dishId,
      })
      if (result.code === 0) {
        this.$message.success('删除成功')
        await this.getDish()
      }
    },

    // 获取菜式id
    getCategoryId() {
      if (!this.categoryList) {
        return
      }
      for (let i = 0; i < this.categoryList.length; i++) {
        if (this.categoryList[i].id === this.category) {
          this.categoryId = this.categoryList[i].id
          this.categoryName = this.categoryList[i].name
        }
      }
    }
  },
  async mounted() {
    await this.getCategory()
    await this.getDish()
  }
}
</script>

<style lang="less" scoped>
.main {
  .add {
    margin-bottom: 10px;
  }
}
</style>